/**
 * Panel sa tekst poljima sa nazivom foldera za upload na klijente.
 */
package klab;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Properties;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import sun.awt.HorizBagLayout;

public class Zadaci extends JPanel {
	private static final long serialVersionUID = 240114705512462307L;
	private String tekstOkvira = "";
	JButton parni   = new JButton("parni..."),
			neparni = new JButton("neparni...");
	JPanel tekst = new JPanel(),
	       polja = new JPanel();
	JTextField parniPath   = new JTextField(),
			   neparniPath = new JTextField();
	JCheckBox isto = new JCheckBox("isto");
	FolderBiraj fBiraj = new FolderBiraj();
	FolderSkraceni fSkraceni = new FolderSkraceni();
	Color pathBackground = new Color(230,230,160);
	
	private Zadaci(){
		super();
	}
	public void izjednaciTekst(){
		if (isto.isSelected())
			parniPath.setText(neparniPath.getText());
	}
	public Zadaci(String naslov){
		this();
		tekstOkvira = naslov;
		init();
	}
	void init(){
		neparniPath.setPreferredSize(new Dimension(300,23));
		parniPath.setPreferredSize(new Dimension(300,23));
		neparniPath.setBackground(pathBackground);					
		parniPath.setBackground(pathBackground);	
		neparniPath.addFocusListener(new FocusAdapter(){
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				izjednaciTekst();
			}			
		});
		
		isto.setPreferredSize(new Dimension(55,23));
		isto.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				if (isto.isSelected()){
					parniPath.setText(neparniPath.getText());
					parniPath.setEnabled(false);
					parniPath.setBackground(Color.lightGray);
					parni.setEnabled(false);
				}else {
					parniPath.setEnabled(true);
					parniPath.setBackground(pathBackground);					
					parni.setEnabled(true);
				}
			}			
		});
		
		JPanel donji = new JPanel();
		donji.setLayout(new HorizBagLayout());
		donji.add(parniPath);
		donji.add(isto);
		donji.setPreferredSize(new Dimension(400,23));
		
		neparni.setPreferredSize(new Dimension(100,23));
		parni.setPreferredSize(new Dimension(100,23));
		neparni.setName("nepar");
		parni.setName("par");
		if (tekstOkvira.length()<30){ // Action listener bez skracivanja patha
			parni.addActionListener(fBiraj);
			neparni.addActionListener(fBiraj);
		}else{                        // Skraceni path bez \\sXY\c$\Documents and settings\
			parni.addActionListener(fSkraceni);
			neparni.addActionListener(fSkraceni);			
		}
		
		tekst.setLayout(new BorderLayout());
		tekst.add(neparni,BorderLayout.NORTH);
		tekst.add(parni,BorderLayout.SOUTH);
		
		polja.setLayout(new BorderLayout());
		polja.add(neparniPath,BorderLayout.NORTH);
		polja.add(donji,BorderLayout.SOUTH);

		setLayout(new BorderLayout());
		setBorder(new TitledBorder(tekstOkvira));
		add(tekst,BorderLayout.WEST);
		add(polja,BorderLayout.EAST);
		setPreferredSize(new Dimension(new Dimension(528,80)));
	}
	class FolderBiraj implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			boolean par=false;
			String neparText = neparniPath.getText(),
				   parText   = parniPath.getText();
			
			if (((JButton) e.getSource()).getName().equals("par")) par = true;			
			
			JFileChooser prozor = new JFileChooser();
			if (par){
				if (!parText.isEmpty() && new File(parText).exists())
					prozor.setCurrentDirectory(new File(parText));
			}else
				if (!neparText.isEmpty() && new File(neparText).exists())
					prozor.setCurrentDirectory(new File(neparText));				
			
			prozor.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			if (prozor.showOpenDialog(Zadaci.this) == JFileChooser.APPROVE_OPTION){
				if (par){
					parniPath.setText(prozor.getSelectedFile().getPath());
				}else{
					neparniPath.setText(prozor.getSelectedFile().getPath());
					if (isto.isSelected()) parniPath.setText(neparText);
				}
			}
		}
	}
	class FolderSkraceni implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			boolean par=false;
			int i=0;
			ReadEnv re = ReadEnv.getInstance();
			Properties p=null;
			try {
				p = re.getEnvVars();
			} catch (Throwable e1) {
				e1.printStackTrace();
			}
			
			String drajv = p.getProperty("HOMEPATH"),
				   zaSkracenje,  rezultat  = "";
			
			zaSkracenje = re.getMainHomePath(false).substring(3);
			
			if (((JButton) e.getSource()).getName().equals("par")) par = true;			
			
			JFileChooser prozor = new JFileChooser();
			prozor.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			prozor.setCurrentDirectory(new File(drajv+zaSkracenje+"\\"));
			if (prozor.showOpenDialog(Zadaci.this) == JFileChooser.APPROVE_OPTION){
				i = prozor.getSelectedFile().getPath().toLowerCase().indexOf(zaSkracenje);
				if (i > -1){
					rezultat = prozor.getSelectedFile().getPath().substring(i+23);
					if (par){
						parniPath.setText(rezultat);
					}else{
						neparniPath.setText(rezultat);
						if (isto.isSelected()) parniPath.setText(neparniPath.getText());
					}
				}
			}
		}
	}
}
